package cassandra;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import utils.ConfigProperties;
import exception.RuleBasedMonitoringException;

public class Emailer extends Action {

	String msg;
	private String severity;
	private String type;
	//	public enum ESeverity {
	//		LOW, MEDIUM, HIGH
	//	}

	public Emailer (String msg) {
		this.msg = msg;
		this.type = "Notification";
	}

	public Emailer (String msg, String severity) {
		this.msg = msg;
		this.type = "Alert";
		this.severity = severity;
	}

	@Override
	public void call() throws RuleBasedMonitoringException {
		super.call();
		sendEmail();		

	}

	public void sendEmail() throws RuleBasedMonitoringException {

		ConfigProperties cProps = ConfigProperties.getInstance();
		String d_email = cProps.getEmailFrom();
		String d_uname = cProps.getUName();
		String d_password = cProps.getPwd();
		String d_host = cProps.getHost();
		String d_port  = cProps.getPort();
		String m_to = cProps.getEmailTo();
		String m_subject = "[Mymed] Cassandra Monitoring " + type;
		if (severity  != null) {  
			m_subject = m_subject + " with severity " + severity;
		}

		String m_text = msg;
		Properties props = new Properties();
		props.put("mail.smtp.user", d_email);
		props.put("mail.smtp.host", d_host);
		props.put("mail.smtp.port", d_port);
		props.put("mail.smtp.starttls.enable","true");
		//props.put("mail.smtp.debug", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", d_port);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		SMTPAuthenticator auth = new SMTPAuthenticator();
		Session session = Session.getInstance(props, auth);
		session.setDebug(true);

		MimeMessage msg = new MimeMessage(session);
		try {
			msg.setText(m_text);
			msg.setSubject(m_subject);
			msg.setFrom(new InternetAddress(d_email));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

			Transport transport = session.getTransport("smtps");
			transport.connect(d_host, Integer.parseInt(d_port), d_uname, d_password);
			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
		} catch (MessagingException e) {
			throw (new RuleBasedMonitoringException(e));
		}


	}

	private class SMTPAuthenticator extends Authenticator
	{
		public PasswordAuthentication getPasswordAuthentication()
		{
			try {
				ConfigProperties cProps = ConfigProperties.getInstance();
				return new PasswordAuthentication(cProps.getEmailFrom(), cProps.getPwd());
			} catch (RuleBasedMonitoringException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}
	}

	@Override
	public String getLaunchedAction() {
		return Emailer.class.getName();
	}


}


